#include "SleeExcel.h"
#include <QDebug>
#include <QFileInfo>
#include <QDir>
#include <QFile>
#include <QTextStream>

SleeExcel::SleeExcel():
    mb_excel_ok(false),
    m_last_id(0)
{
}

quint32 SleeExcel::gen_id()
{
    return ++m_last_id;
}

bool SleeExcel::is_id_ok(quint32 id)
{
    return  mqh_books.keys().contains(id);
}

void SleeExcel::run_excel()
{
    if(mb_excel_ok) return;
    m_excel.mp = new QAxObject ("Excel.Application");
    m_exbooks.mp = m_excel.mp->querySubObject("Workbooks");
    mb_excel_ok = m_exbooks.is_ok();
}

void SleeExcel::quit_excel()
{
    if(!mb_excel_ok) return;
    foreach(SleeExcelBook *p_exbook, mqh_books)
    {
        p_exbook->close();
        delete p_exbook;
    }
    m_exbooks.free_ptr();
    m_excel.mp->dynamicCall("Quit()");
    m_excel.free_ptr();
    mb_excel_ok = false;
}

void SleeExcel::set_visible(bool state)
{
    if(!mb_excel_ok) return;
    m_excel.mp->dynamicCall("SetVisible(bool)", state);
}


SleeExcelBook *SleeExcel::open_workbook(QString file_pathname)
{
    if(!QFile::exists(file_pathname))
    {
        qCritical()<<"requested file doesn't exists";
        return NULL;
    }
    if(!mb_excel_ok) run_excel();
    if(!mb_excel_ok) return NULL;

    quint32 id = gen_id();
    SleeQAxPtr exbook;
    exbook.mp = m_exbooks.mp->querySubObject("Open(const QString &)", file_pathname);
    if(!exbook.is_ok())
    {
        qDebug()<<"unable to open requested workbook";
        return 0;
    }
    mqh_books[id] = new SleeExcelBook(exbook,id);
    return mqh_books[id];
}

SleeExcelBook *SleeExcel::new_workbook()
{
    if(!mb_excel_ok) run_excel();
    if(!mb_excel_ok) return 0;
    QAxObject *p_workbooks = m_excel.mp->querySubObject("Workbooks");
    if(p_workbooks->isNull())
    {
        qDebug()<<"unable to get workbooks";
        return 0;
    }
    quint32 id = gen_id();
    SleeQAxPtr exbook;
    exbook.mp = m_exbooks.mp->querySubObject("Add");
    if(!exbook.is_ok())
    {
        qDebug()<<"unable to open requested workbook";
        return 0;
    }
    mqh_books[id] = new SleeExcelBook(exbook,id);
    return mqh_books[id];
}

void SleeExcel::close_workbook(SleeExcelBook **pp_exbook)
{
    if((*pp_exbook) == NULL) return;
    quint32 id = (*pp_exbook)->get_id();
    if(!is_id_ok(id)) return;
    (*pp_exbook)->close();
    delete (*pp_exbook);
    mqh_books.remove(id);
    (*pp_exbook) = NULL;
}


void SleeExcel::generate_documentation(const QDir &dir)
{
    if(!mb_excel_ok) return;
    QFile out_file;
    QString filename;
    filename = dir.path() + "/00_excel.html";
    out_file.setFileName(filename);
    out_file.open(QIODevice::WriteOnly);
    QTextStream out;
    out.setDevice(&out_file);
    out<<m_excel.mp->generateDocumentation();
    out_file.close();

    QAxObject *p_wrkbks = m_excel.mp->querySubObject("Workbooks");
    filename = dir.path()+"/01_workbooks.html";
    out_file.setFileName(filename);
    out_file.open(QIODevice::WriteOnly);
    out.setDevice(&out_file);
    out<<p_wrkbks->generateDocumentation();
    out_file.close();

    QAxObject *p_wrkbk = p_wrkbks->querySubObject("Add");
    filename = dir.path()+"/02_workbook.html";
    out_file.setFileName(filename);
    out_file.open(QIODevice::WriteOnly);
    out.setDevice(&out_file);
    out<<p_wrkbk->generateDocumentation();
    out_file.close();

    QAxObject *p_sheets = p_wrkbk->querySubObject("Sheets");
    filename = dir.path()+"/03_sheets.html";
    out_file.setFileName(filename);
    out_file.open(QIODevice::WriteOnly);
    out.setDevice(&out_file);
    out<<p_sheets->generateDocumentation();
    out_file.close();

    QVariant qv_index = 1;
    QAxObject *p_sheet = p_sheets->querySubObject("Item(const QVariant &)", qv_index);
    filename = dir.path()+"/04_sheet.html";
    out_file.setFileName(filename);
    out_file.open(QIODevice::WriteOnly);
    out.setDevice(&out_file);
    out<<p_sheet->generateDocumentation();
    out_file.close();

    QVariant r = 1, c = 1;

    QAxObject *p_cell = p_sheet->querySubObject("Cells(QVariant, QVariant)", r, c);
    filename = dir.path()+"/05_cells.html";
    out_file.setFileName(filename);
    out_file.open(QIODevice::WriteOnly);
    out.setDevice(&out_file);
    out<<p_cell->generateDocumentation();
    out_file.close();
    QVariant qv_data = QString::fromUtf8("привет мир");
    p_cell->setProperty("Value",qv_data);

    p_wrkbk->dynamicCall("Close(const QVariant &)", QVariant(0));
    delete p_cell;
    delete p_sheet;
    delete p_sheets;
    delete p_wrkbk;
    delete p_wrkbks;
}
